package com.horizon.entity;

import java.io.Serializable;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Administrator
 * @date 2021/9/4
 */
@Data
@Slf4j
public class ImNode implements Comparable<ImNode>, Serializable {
    // worker的id，zk负责生成
    private long id;

    // Netty服务的连接数
    private AtomicInteger balance = new AtomicInteger(0);

    // Netty服务的ip
    private String host;

    // Netty服务的端口
    private Integer port;

    @Override
    public int compareTo(ImNode o) {
        int weight1 = this.balance.get();
        int weight2 = o.balance.get();
        if (weight1 > weight2) {
            return 1;
        } else if (weight1 < weight2) {
            return -1;
        }
        return 0;
    }

    public ImNode() {}

    public ImNode(String host, Integer port) {
        this.host = host;
        this.port = port;
    }

    @Override
    public String toString() {
        return "ImNode{" + "id=" + id + ", balance=" + balance + ", host='" + host + '\'' + ", port=" + port + '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;
        ImNode imNode = (ImNode)o;
        return id == imNode.id && Objects.equals(host, imNode.host) && Objects.equals(port, imNode.port);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, host, port);
    }
}
